ERC-20 and ERC-721 Token Standards


1. Introduction to ERC and EIP

Key ERCs:


2. ERC-20 Token Standard (Fungible Tokens)

Overview:

ERC-20 Functions & Events:

Each ERC-20 token implements a set of mandatory functions to ensure compatibility.

Function Description
totalSupply() Returns the total supply of tokens.
balanceOf(address account) Returns the token balance of a specific address.
transfer(address to, uint256 amount) Transfers tokens from the caller’s address to another.
transferFrom(address from, address to, uint256 amount) Transfers tokens from one address to another on behalf of an address that approved the sender.
approve(address spender, uint256 amount) Allows a spender to withdraw tokens from the caller’s account multiple times, up to a specified amount.
allowance(address owner, address spender) Returns the remaining number of tokens that the spender is allowed to spend on behalf of the owner.
Events The ERC-20 standard requires the emission of certain events to reflect actions taken by the token.
Transfer(address from, address to, uint256 amount) Event triggered when tokens are transferred.
Approval(address owner, address spender, uint256 amount) Event triggered when the approve() function is called, notifying the network of the approval.

In-Depth Explanation of Functions:

  1. totalSupply():

    • Definition: Returns the total amount of tokens in existence.
    • Usage: Essential for tracking how many tokens exist within the system, which helps manage scarcity and ensures that the smart contract knows how many tokens are being circulated.
    function totalSupply() public view returns (uint256);
    
  2. balanceOf(address account):

    • Definition: Provides the balance of a specific address.
    • Usage: Allows users and smart contracts to check how many tokens a given address holds. The balance is stored as an unsigned integer (uint256).
    function balanceOf(address account) public view returns (uint256);
    
  3. transfer(address to, uint256 amount):

    • Definition: Transfers a specified amount of tokens to another address.
    • Usage: This is the basic method for moving tokens between two addresses.
    function transfer(address to, uint256 amount) public returns (bool);
    
  4. transferFrom(address from, address to, uint256 amount):

    • Definition: Transfers tokens on behalf of an account.
    • Usage: Used in situations like escrow or when allowing a smart contract to spend tokens on behalf of a user.
    function transferFrom(address from, address to, uint256 amount) public returns (bool);
    
  5. approve(address spender, uint256 amount):

    • Definition: Grants another address permission to spend up to a certain number of tokens on the caller’s behalf.
    • Usage: Allows contracts like DEXs or lending protocols to handle tokens on behalf of users.
    function approve(address spender, uint256 amount) public returns (bool);
    
  6. allowance(address owner, address spender):

    • Definition: Returns the amount a spender is allowed to withdraw from the owner’s account.
    • Usage: Useful to check how many tokens a contract or address can spend on behalf of the owner.
    function allowance(address owner, address spender) public view returns (uint256);
    
  7. Events (Transfer & Approval):

    • Transfer Event: Emitted whenever a transfer of tokens occurs, allowing applications to track transactions.
    • Approval Event: Emitted when approve() is called, allowing applications to track approvals.

3. ERC-721 Token Standard (Non-Fungible Tokens - NFTs)

Overview:

ERC-721 Functions & Events:

ERC-721 includes many of the same functions as ERC-20 but with additional features for managing individual tokens, since each one is unique.

Function Description
balanceOf(address owner) Returns the number of NFTs owned by a specific address.
ownerOf(uint256 tokenId) Returns the owner of a particular token.
transferFrom(address from, address to, uint256 tokenId) Transfers ownership of a specific NFT.
approve(address to, uint256 tokenId) Approves another address to transfer a specific token on behalf of the owner.
getApproved(uint256 tokenId) Returns the approved address for a specific token.
setApprovalForAll(address operator, bool approved) Approves or removes an operator (typically for managing multiple tokens).
isApprovedForAll(address owner, address operator) Checks if an operator is approved to manage all of the owner’s assets.
Events Events help in tracking changes and interactions with the NFTs.
Transfer(address from, address to, uint256 tokenId) Event triggered when an NFT is transferred.
Approval(address owner, address approved, uint256 tokenId) Event triggered when an NFT’s approval is updated.

In-Depth Explanation of Functions:

  1. balanceOf(address owner):

    • Definition: Returns the number of NFTs owned by an address.
    • Usage: Useful for tracking how many unique NFTs a user possesses.
    function balanceOf(address owner) public view returns (uint256);
    
  2. ownerOf(uint256 tokenId):

    • Definition: Returns the owner of a specific token.
    • Usage: Essential for determining who owns a particular NFT.
    function ownerOf(uint256 tokenId) public view returns (address);
    
  3. transferFrom(address from, address to, uint256 tokenId):

    • Definition: Transfers a specific NFT from one address to another.
    • Usage: Basic function to facilitate the movement of NFTs between users.
    function transferFrom(address from, address to, uint256 tokenId) public;
    
  4. approve(address to, uint256 tokenId):

    • Definition: Approves another address to transfer a particular NFT.
    • Usage: Allows another address or contract to manage a specific NFT.
    function approve(address to, uint256 tokenId) public;
    
  5. getApproved(uint256 tokenId):

    • Definition: Returns the approved address for a specific NFT.
    • Usage: Useful for tracking delegation of ownership or management rights over a specific token.
    function getApproved(uint256 tokenId) public view returns (address);
    
  6. setApprovalForAll(address operator, bool approved):

    • Definition: Allows an operator to manage all of the owner’s NFTs.
    • Usage: Simplifies asset management by allowing an operator to manage all NFTs on behalf of the user.
    function setApprovalForAll(address operator, bool approved) public;
    
  7. isApprovedForAll(address owner, address operator):

    • Definition: Checks if an operator is approved to manage all of the owner’s NFTs.
    • Usage: Used to verify if an operator has universal management rights.
    function isApprovedForAll(address owner, address operator) public view returns (bool);
    
  8. Events (Transfer & Approval):

    • Transfer Event: Emitted when ownership of a token changes.
    • **Approval Event

**: Emitted when a token’s approved operator is changed.


4. Differences Between ERC-20 and ERC-721

Aspect ERC-20 ERC-721
Type of Token Fungible Non-fungible
Uniqueness Tokens are identical and interchangeable Each token is unique and cannot be exchanged on a 1:1 basis
Use Cases Cryptocurrencies, utility tokens NFTs, digital art, collectibles
Divisibility Can be fractional (e.g., 0.1 ETH) Indivisible, must be transferred as whole tokens
Metadata No unique metadata per token Unique metadata for each token
Events Transfer, Approval Transfer, Approval, ApprovalForAll